<?php

namespace app\api\controller\dz\index;

use app\ApiBaseController;
use app\model\ConsultOfflineModel;
use app\model\ConsultOnlineModel;
use app\model\ConsultQuestionModel;
use app\model\EvaluationLogModel;
use think\App;

class Index extends ApiBaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 获取测评记录数据
     * @return bool|string
     */
    public function getEvaluationLog(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'start_time|开始时间' => 'require',
                'end_time|结束时间' => 'require',
            ]);
            $start_time = strtotime($param['start_time']);
            $end_time = strtotime($param['end_time']);
            $where[] = ['time','between',[$start_time,$end_time]];
            $where[] = ['type','in',[1,2]];
            $where[] = ['unit_id','=',24];
            $EvaluationLogModel = new EvaluationLogModel();
            $data = $EvaluationLogModel->field('id,uid,unit_id,rating_scale_id,type,finish_used_time,time')->where($where)->with([
                'ratingScale',
                'user'
            ])->select()->toArray();
            foreach ($data as &$item) {
                $item['guidance'] = '';
                $item['time'] = date('Ymd',$item['time']);
            }
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 获取咨询记录数据
     * @return bool|string
     */
    public function getConsultLog(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'date|时间（YYYYMMDD）' => 'require',
            ]);
            $start_time = strtotime(date($param['date'].' 00:00:00'));
            $end_time = strtotime(date($param['date'].' 23:59:59'));
            $ConsultOfflineModel = new ConsultOfflineModel();
            $ConsultOnlineModel = new ConsultOnlineModel();
            $ConsultQuestionModel = new ConsultQuestionModel();

            $question_id = $ConsultOfflineModel->where([
                ['unit_id','=',24],
                ['time','between',[$start_time,$end_time]],
            ])->distinct(true)->column('question_id');
            $question_id2 = $ConsultOnlineModel->where([
                ['unit_id','=',24],
                ['time','between',[$start_time,$end_time]],
            ])->distinct(true)->column('question_id');
            $question_id = array_unique(array_merge($question_id,$question_id2));
            $keyword = '';
            if (!empty($question_id)) {
                $keyword = $ConsultQuestionModel->where('id','in',$question_id)->column('name');
                $keyword = implode(',',$keyword);
            }
            $data = [
                'keyword' => $keyword,
                'time' => $param['date'],
                'online' => $ConsultOnlineModel->where([
                    ['unit_id','=',24],
                    ['time','between',[$start_time,$end_time]],
                ])->distinct(true)->count('uid'),
                'offline' => $ConsultOfflineModel->where([
                    ['unit_id','=',24],
                    ['time','between',[$start_time,$end_time]],
                ])->distinct(true)->count('uid')
            ];
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }


}